﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Table_Dal;
using System.Data;
using System.Data.SqlClient;
namespace DAL
{
    public class NguoiradeDAL : DataAccessLayer
    {
        DataDataContext dt;
        public string PictureLink(string MaNguoirade)
        {
            dt = GetKetnoi();
            NGUOIRADE da = dt.NGUOIRADEs.SingleOrDefault(i => i.MANGUOIRADE == MaNguoirade);
            if (da.HINHANH.Trim().Length == 0 || da.HINHANH == null)
                return "";
            else
                return da.HINHANH;
        }
        public string Madangnhap(string id)
        {
            dt = GetKetnoi();
            NGUOIRADE ng = dt.NGUOIRADEs.SingleOrDefault(i => i.MANGUOIRADE == id);
            return ng.MADANGNHAP;

        }
        public string Manguoirade(string id)
        {
            dt = GetKetnoi();
            NGUOIRADE ng = dt.NGUOIRADEs.SingleOrDefault(i => i.MADANGNHAP == id);
            return ng.MANGUOIRADE;

        }
        public NguoiradeTbl ThongTin(string ID)
        {
            dt = GetKetnoi();
            NGUOIRADE ng = dt.NGUOIRADEs.SingleOrDefault(i => i.MADANGNHAP == ID);
            NguoiradeTbl Nrd = new NguoiradeTbl();
            Nrd.MADANGNHAP = ID;
            Nrd.TENNGUOIRADE = ng.TENNGUOIRADE;
            Nrd.GIOITINH = int.Parse(ng.GIOITINH.ToString());
            Nrd.DIACHI = ng.DIACHI;
            Nrd.HINHANH = ng.HINHANH;
            Nrd.MANGUOIRADE = ng.MANGUOIRADE;
            Nrd.NGAYSINH = DateTime.Parse (ng.NGAYSINH.ToString());

            QuantriDal qt = new QuantriDal();
            Nrd.MATKHAU = qt.MatKhau(Nrd.MADANGNHAP);
            
            return Nrd;
        }
        public bool ThemNguoirade(NguoiradeTbl Nrd)
        {

            try
            {
                dt = GetKetnoi();
                NGUOIRADE ng = new NGUOIRADE();
                ng.MANGUOIRADE = Nrd.MANGUOIRADE;
                ng.TENNGUOIRADE = Nrd.TENNGUOIRADE;
                ng.HINHANH = Nrd.HINHANH;
                ng.NGAYSINH = DateTime.Parse(Nrd.NGAYSINH.ToString());
                ng.DIACHI = Nrd.DIACHI;
                ng.GIOITINH = int.Parse(Nrd.GIOITINH.ToString());
                ng.MADANGNHAP = Nrd.MADANGNHAP;
                dt.NGUOIRADEs.InsertOnSubmit(ng);
                dt.SubmitChanges();
            }
            catch
            {
                return false;
            }
            return true;
        }
        public bool Xoa(string nguoirade)
        {
            try
            {
                string madangnhap = "";
                dt = GetKetnoi();
                NGUOIRADE nrd = dt.NGUOIRADEs.SingleOrDefault(i => i.MANGUOIRADE == nguoirade);
                madangnhap = nrd.MADANGNHAP;
                dt.NGUOIRADEs.DeleteOnSubmit(nrd);
                QUANTRI qt = dt.QUANTRIs.SingleOrDefault(i => i.MADANGNHAP == madangnhap);
                dt.QUANTRIs.DeleteOnSubmit(qt);
                dt.SubmitChanges();

            }
            catch
            {
                return false;
            }
            return true;
        }
        public List<NguoiradeTbl> DSNguoiRade_PhanQuyen(int Quyen)
        {
            dt = GetKetnoi();
            IEnumerable<NGUOIRADE> lst = from i in dt.NGUOIRADEs
                                         join j in dt.QUANTRIs
                                         on i.MADANGNHAP equals j.MADANGNHAP
                                         where j.PHANQUYEN == 1
                                         select i;
            List<NguoiradeTbl> ds = new List<NguoiradeTbl>();
            foreach (NGUOIRADE item in lst)
            { 
                NguoiradeTbl nrd = new NguoiradeTbl();
                nrd.MANGUOIRADE = item.MANGUOIRADE;
                nrd.TENNGUOIRADE = item.TENNGUOIRADE;
                nrd.GIOITINH = int.Parse( item.GIOITINH.ToString());
                nrd.PHANQUYEN = 1;
                nrd.NGAYSINH = DateTime.Parse( item.NGAYSINH.ToString());
                nrd.DIACHI = item.DIACHI;
                ds.Add(nrd);
            }
            return ds;
        }
        public bool Sua(NguoiradeTbl Nguoirade)
        {
            try
            {

                DataDataContext Context = GetKetnoi();
                NGUOIRADE qt = Context.NGUOIRADEs.SingleOrDefault(i => i.MADANGNHAP == Nguoirade.MADANGNHAP);
                qt.TENNGUOIRADE = Nguoirade.TENNGUOIRADE;
                qt.GIOITINH= Nguoirade.GIOITINH;
                qt.HINHANH= Nguoirade.HINHANH;
                qt.DIACHI= Nguoirade.DIACHI;
                qt.NGAYSINH= Nguoirade.NGAYSINH;

                Context.SubmitChanges();
            }
            catch
            {
                return false;
            }
            return true;
        }
        public bool KiemTra_TrungKhoa(string Ma)
        {
            dt = GetKetnoi();
            IEnumerable<NGUOIRADE> q = dt.NGUOIRADEs.Where(k => k.MANGUOIRADE == Ma);
            foreach (NGUOIRADE item in q)
            {
                if (item.MANGUOIRADE == Ma)
                    return true;
            }
            return false;
        }
    }
}